package com.java.controller;

import com.java.pojo.OneMenu;
import com.java.service.SysAuthorityService;
import com.java.service.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @function:
 * @CreateTime: 2021/3/16 14:07
 */
@Controller
public class SystemUserController {
    @Autowired
    private SysAuthorityService sysAuthorityService;

    @Autowired
    private SystemUserService systemUserService;

    /**
     * 进入添加用户信息页面,查询权限信息
     * @param model
     * @return
     */
    @RequestMapping("toAddUser")
    public String toAddUser(Model model){
        List<OneMenu> sqAuthorityList = sysAuthorityService.selectSQAuthority();
        model.addAttribute("sqAuthorityList",sqAuthorityList);
        return "/user/addSystemUser.jsp";
    }

    /**
     * 添加用户信息以及用户权限
     * @param username
     * @param pwd
     * @param oneIds
     * @param twoIds
     * @return
     */
    @RequestMapping("addSystemUser")
    @ResponseBody
    public boolean addSystemUser(String username,String pwd,String oneIds,String twoIds){
        boolean blg = systemUserService.addSystemUser(username, pwd, oneIds, twoIds);
        return blg;
    }

    /**
     *  分页查询用户信息
     * @param model
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/getSystemUserByLimit")
    public String getSystemUserByLimit(Model model, @RequestParam(defaultValue = "1") String pageNum,@RequestParam(defaultValue = "10") String pageSize){
        List<Map<String, Object>> maps = systemUserService.selectSystemUserAll(pageNum, pageSize);
        model.addAttribute("systemUserList",maps);
        return "/user/showSystemUser.jsp";
    }

    /**
     * 修改用户状态
     * @param userId
     * @param flag
     * @return
     */
    @RequestMapping("updateSystemUserStatus")
    public String updateSystemUserStatus(Long userId,String flag){
        int status = Integer.parseInt(flag);
        if(status > 1 || status < 0)
            return "redirect:/getSystemUserByLimit.do";
        systemUserService.updateSystemUserStatus(userId,flag);
        return "redirect:/getSystemUserByLimit.do";
     }
}
